<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use DB;
use App\Models\UserModels\User;
use App\Components\Tools;
use App\Components\DyToools;
use App\Models\DyModels\Douyin;
use App\Models\DyModels\DyuserQyhVideo;

/**
 * 获取授权用户视频
 */
class DyEnterpriseVideo extends Command {

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'DyEnterpriseVideo';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '获取授权用户视频';
    private $url = '';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle() {
        Tools::commandsLog('DyEnterpriseVideo:' . date("Y-m-d H:i:s"));
        $this->url = Tools::getOneconfig("/config/third.json", array('douyin', 'dy_url'));
        $this->getEnterpriseinfo();
    }

    //获取企业用户视频数据
    function getEnterpriseinfo() {
        $soDyuserQyhVideo = new DyuserQyhVideo();
        $soUser = new User();
        $infoToken = new Douyin();
        $DyToools = new DyToools();
        $openid = $soUser->getuserinfo(); //获取openid和对应的access_token以及refresh_token
        foreach ($openid as $k => $v) {
            if (!empty($v['access_token'])) {
                $url = $this->url . "/video/list/?open_id={$v['open_id']}&access_token={$v['access_token']}&count=20"; //获取近一个月的视频
                $data = $this->getJson($url);
                if ($data['extra']['error_code'] == 0) {
                    // 返回值正确的话  数据入库
                    $soDyuserQyhVideo->saveVideoInfo($data['data']['list'], $v['open_id']);
                    if ($data['data']['has_more'] == 1) {
                        //如果还有更多视频没拉取调用方法拉取
                        $this->getMoreVideolist($v['open_id'], $v['access_token'], $data['data']['cursor']); //如果有就调用函数把所有的数据都抓取出来
                    }
                } elseif (isset($dataList['data']['error_code']) && ($dataList['data']['error_code'] == 10008 || $dataList['data']['error_code'] == 2190008)) {
                    //授权过期刷新access_token然后继续
                    $ass = $this->refresh_token($v['refresh_token']); //刷新access——token
                    $infoToken->upToken($ass['data'], $v['open_id']); //更新数据库的信息
                    //重新调用获取数据的方法
                    if ($ass['data']['error_code'] == 0) { //如果返回值正确就进行接口调用
                        $url = $this->url . "/video/list/?open_id={$v['open_id']}&access_token={$ass['data']['access_token']}&count=20"; //获取近一个月的视频
                        $date = $this->getJson($url);
                        if ($date['extra']['error_code'] == 0) {// 返回值正确的话  数据入库
                            $soDyuserQyhVideo->saveVideoInfo($date['data']['list'], $v['open_id']);
                            if ($date['data']['has_more'] == 1) {
                                //如果还有更多视频没拉取调用方法拉取
                                $this->getMoreVideolist($v['open_id'], $ass['data']['access_token'], $date['data']['cursor']); //如果有就调用函数把所有的数据都抓取出来
                            }
                        }
                    } elseif (isset($ass['data']['error_code']) && $ass['data']['error_code'] == 10010) {//如果之前刷新也失败返回这个就调用方法目前无效 需重新授权
                        //refresh_token 过期
                        $dataTwo = $DyToools->renew_refresh_token($v['refresh_token']); //刷新refresh_token
                        if (isset($data['data']['error_code']) && $dataTwo['data']['error_code'] == 0) {
                            //更新数据库的 refresh_token
                            $infoToken->upRef($dataTwo['data'], $v['open_id']);
                            //用新的来更新access_token 然后继续调用数据获取下视频数据
                            $acss = $this->refresh_token($dataTwo['data']['refresh_token']); //刷新access——token
                            $infoToken->upToken($acss['data'], $v['open_id']); //更新数据库的信息
                            $urel = $this->url . "/video/list/?open_id={$v['open_id']}&access_token={$acss['data']['access_token']}&count=20"; //获取近一个月的视频
                            $datt = $this->getJson($urel);
                            if ($datt['extra']['error_code'] == 0) {// 返回值正确的话  数据入库
                                $soDyuserQyhVideo->saveVideoInfo($datt['data']['list'], $v['open_id']);
                                if ($datt['data']['has_more'] == 1) {
                                    //如果还有更多视频没拉取调用方法拉取
                                    $this->getMoreVideolist($v['open_id'], $acss['data']['access_token'], $datt['data']['cursor']); //如果有就调用函数把所有的数据都抓取出来
                                } else {
                                    $infoToken->upTokenStatus($v['open_id']); //授权过期需用户重新授权 更改授权状态 为 0
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    //获取所有的视频的方法
    function getMoreVideolist($open_id, $access_token, $cursor) {
        $soDyuserQyhVideo = new DyuserQyhVideo();
        $url = $this->url . "/video/list/?open_id=$open_id&access_token=$access_token&cursor=$cursor&count=20"; //获取近一个月的视频
        $data = $this->getJson($url);
        if ($data['data']['has_more'] == 1) {
            //先数据入库然后再次调用自己
            $soDyuserQyhVideo->saveVideoInfo($data['data']['list'], $open_id);
            $this->getMoreVideolist($open_id, $access_token, $data['data']['cursor']);
        } else {
            $soDyuserQyhVideo->saveVideoInfo($data['data']['list'], $open_id);
        }
    }

    //get请求
    function getJson($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //为true，则会跟踪爬取重定向页面，否则，不会跟踪重定向页面
        curl_setopt($ch, CURLOPT_ENCODING, "");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书下同
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $output = curl_exec($ch);
        curl_close($ch);
        return json_decode($output, true);
    }

}
